package com.coinworld.response;

import com.alibaba.fastjson.JSONObject;
import com.coinworld.page.PageResult;
import lombok.Data;

/**
 * Result
 *
 * @author
 * @date 2019/11/8
 * @description
 */
@Data
public class Result<T> {

    private T data;

    private String msg;

    private Long total;

    private Integer code;


    public Result() {

    }

    public Result(Integer code) {
        this.code = code;
    }

    public Result(Integer code, T data) {
        this.code = code;
        this.data = data;
    }

    public Result(Integer code, String mes) {
        this.code = code;
        this.msg = mes;

    }

    public Result(Integer code, String mes, T data) {
        super();
        this.code = code;
        this.msg = mes;
        this.data = data;
    }

    public Result(Integer code, T data,Long total) {
        super();
        this.code = code;
        this.data = data;
        this.total = total;
    }

    public Result(Integer code, String mes, T data,Long total) {
        super();
        this.code = code;
        this.msg = mes;
        this.data = data;
        this.total = total;
    }

    public static Result success(){
        return new Result(ResponseCode.Common.SUCCESS.getCode(),null);
    }
    public static <T>  Result success(T data){
        return new Result(ResponseCode.Common.SUCCESS.getCode(),JSONObject.toJSON(data));
    }
    public static <T>  Result success(PageResult<T> data){
        return new Result(ResponseCode.Common.SUCCESS.getCode(), JSONObject.toJSON(data.getResults()),data.getTotal());
    }

    public static Result fail(Integer code){
        return new Result(code,ResponseCode.Common.getName(code));
    }
    public static Result fail(Integer code,String mes){
        return new Result(code,mes);
    }
    public static <T> Result fail(Integer code,T data){
        return new Result(code,JSONObject.toJSON(data));
    }


}
